package com.mz3co.sysadmin.page;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.dom.xml.Element;

import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.ShellRunner;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;


 
public class AddDoubleQuotesOnColumnPlugin extends PluginAdapter {

    @Override
    public void initialized(IntrospectedTable introspectedTable) {
    	List<IntrospectedColumn> cl=introspectedTable.getAllColumns();
    	for(IntrospectedColumn c:cl){
    		c.setActualColumnName("\""+c.getActualColumnName().toLowerCase()+"\"");
    	}
    }
    
    @Override
    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,
            IntrospectedTable introspectedTable){
    	List <IntrospectedColumn> columns= introspectedTable.getAllColumns();
    	List <String > names = new ArrayList<String>();
    	List <String > names2 = new ArrayList<String>();
    	for (int j=0;j<columns.size();j++)
    	{
    		names.add(columns.get(j).getActualColumnName());
    		names2.add(columns.get(j).getActualColumnName().replace("\"", "-doubleQuotes-"));
    	}
    	
    	List<InnerClass> innerC=topLevelClass.getInnerClasses();
    	for(InnerClass in:innerC){
    		
    		for(Method m:in.getMethods())
        	{
        		List<String> lines=new ArrayList();
        		for(String s:m.getBodyLines())
        		{
        			lines.add(s);
        		}
        		m.getBodyLines().clear();
        		for(String s:lines)
        		{
        			for(int i=0;i<names.size();i++)
        			{
        				String am=names.get(i);
        				String am2=names2.get(i);
        				if(s.contains(am))
        				s=s.replaceFirst(am, am2);//-----------------------
        			}
        			m.addBodyLine(s.replace("-doubleQuotes-", "\\\""));
        		}
        	}
    	}
    	boolean re=super.modelExampleClassGenerated(topLevelClass,introspectedTable);
        return re;
    }


    @Override
    public boolean sqlMapResultMapWithBLOBsElementGenerated(XmlElement element,
            IntrospectedTable introspectedTable) {
    	List <Element> eList=element.getElements();
    	for(Element e:eList)
    	{
    		String s=e.getFormattedContent(1);
    		s="233434";
    	}
        return super.sqlMapResultMapWithBLOBsElementGenerated(element,introspectedTable);
    }
    
    @Override
    public boolean sqlMapResultMapWithoutBLOBsElementGenerated(
            XmlElement element, IntrospectedTable introspectedTable) {
    	List <Element> eList=element.getElements();
    	for(Element e:eList)
    	{
    		XmlElement eX=(XmlElement)e;
    		List<Attribute> aList=eX.getAttributes();
    		int index=-1;
    		for(int i=0;i<aList.size();i++)
    		{
    			if(aList.get(i).getName().equals("column"))
    			{
    				index=i;
    			}
    		}
    		Attribute newa=new Attribute("column",aList.get(index).getValue().replace("\"", ""));
    		aList.remove(index);
    		aList.add(0, newa);
    	}
        return super.sqlMapResultMapWithoutBLOBsElementGenerated(element,introspectedTable);
    }

	@Override
	public boolean validate(List<String> warnings) {
		// TODO Auto-generated method stub
		return true;
	}

    
}